

package com.hazelcast.config.security;

import com.hazelcast.security.Credentials;
import com.hazelcast.security.ICredentialsFactory;

import java.util.Objects;

import static java.util.Objects.requireNonNull;

/**
 * Identity configuration which just holds given credentials instance.
 */
public class CredentialsIdentityConfig implements IdentityConfig {

    private final Credentials credentials;

    public CredentialsIdentityConfig(Credentials credentials) {
        this.credentials = requireNonNull(credentials);
    }

    public Credentials getCredentials() {
        return credentials;
    }

    @Override
    public IdentityConfig copy() {
        return new CredentialsIdentityConfig(credentials);
    }

    @Override
    public ICredentialsFactory asCredentialsFactory(ClassLoader cl) {
        return new StaticCredentialsFactory(credentials);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("CredentialsIdentityConfig [credentials=").append(credentials).append("]");
        return builder.toString();
    }

    @Override
    public int hashCode() {
        return Objects.hash(credentials);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        CredentialsIdentityConfig other = (CredentialsIdentityConfig) obj;
        return Objects.equals(credentials, other.credentials);
    }
}
